package com.github.minglie.mingapiutils.utils;


import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.util.StringUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class DbUtils {

    public static JdbcTemplate jdbcTemplate=null;

    static {
        DriverManagerDataSource dataSource=new DriverManagerDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/miapi?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public static   List<Map<String, Object>> doSql(String sql){
        List<Map<String, Object>> maps=new ArrayList<>();
        if(StringUtils.isEmpty(sql)){
            return maps;
        }
        sql=sql.trim();
        if(sql.startsWith("select")||sql.startsWith("SELECT")){
            maps = jdbcTemplate.queryForList(sql);
        }else {
            jdbcTemplate.execute(sql);
        }
        return maps;
    }
    public static String getByApiName(String apiName){
        String sql= MessageFormat.format(ApiUtilsConstants.QUERY_API_SQL, apiName);
        List<Map<String, Object>> maps = DbUtils.doSql(sql);
        Object exmple = maps.get(0).get("exmple");
        return exmple.toString();
    }


    public static void main(String[] args) {
        String a = getByApiName("接口列表");
        System.out.println(a);
    }

}
